<?php 
// 引入必要文件
include 'codeBack.config.php';
include 'Token.php';
include 'empty.php';
include 'connectMysql.php';
include 'header.php';
// 前要声明
$username = $_POST['user_name'];
$password = $_POST['user_pass'];
$userimg = "http://lorempixel.com/84/84/nature/";
$token = $token->product_token();
if(!$username || !$password || !$token || !$userimg){
	exit();
}
// 用户注册
// 先查询是否被注册过了
$sql = "SELECT * FROM ac_user WHERE user_name='$username'";
$result = $mysqli->query($sql);
$row = mysqli_fetch_object($result);
if(!$row->user_name){
	// 表示没有数据
	// 将数据插入数据库中
	$password = md5($password);
	$sql = "INSERT INTO  ac_user (user_name, user_pass, user_img, token) VALUES ('$username', '$password', '$userimg', '$token')";
	$result = $mysqli->query($sql);
	// 获取插入id
	$insert_id = $mysqli->insert_id;
	if($result){
		// 注册成功
		$data = [
			"code" => $CodeBack["REGISTER_SUCCESS_CODE"],
			"user_name" => $username,
			"message" => "注册成功"
		];
	}else {
		// 插入不成功，就是注册失败
		$data = [
			"code" => $CodeBack["REGISTER_FAILED_CODE"],
			"message"  => "注册失败",
		];
	}
	echo json_encode($data);
}else {
	if($row->user_name == $username){
		// 说明被人注册过了
		// 注册失败
		$data = [
			"code" => $CodeBack["REGISTER_FAILED_CODE"],
			"message"  => "昵称已被注册过，请换一个",
		];
		echo json_encode($data);
	}
}

// 安全判断
// 1 账号没被注册过，注册成功
// 2 账号被注册过，注册失败
